package cn.wgx.module.mind.websocket.util;


import cn.wgx.common.util.SpringContextHolder;
import cn.wgx.module.mind.websocket.entity.BaseMessage;
import cn.wgx.module.mind.websocket.entity.MessageType;
import cn.wgx.module.mind.websocket.entity.WSPrincipal;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.LinkedList;
import java.util.List;

/**
 * 聊天室在线用户管理工具类
 */
public class OnlineUserTool {


    private static RedisTemplate<String, WSPrincipal> redisTemplate = SpringContextHolder.getBean("redisTemplate");

    private static String ROOM_KEY = "ONLINE_ROOM_";

    private final static List<WSPrincipal> EMPTY = new LinkedList<>();

    private static HashOperations<String, String, WSPrincipal> getSet(){
        return redisTemplate.opsForHash();
    }

    //房间加人
    public static boolean addUser(String roomId, WSPrincipal wsPrincipal){
        if(roomId == null || wsPrincipal == null){
            return false;
        }
        getSet().put(ROOM_KEY + roomId, wsPrincipal.getName(), wsPrincipal);
        BaseMessage baseMessage = new BaseMessage(wsPrincipal);
        baseMessage.setMsgType(MessageType.EVENT_USER_JOIN);
        baseMessage.setContent("加入房间");
        RoomMsgSender.sendPublicMsg(roomId, baseMessage);
        return true;
    }

    //房间退出
    public static boolean delUser(String roomId, WSPrincipal wsPrincipal){
        if(roomId == null || wsPrincipal == null){
            return false;
        }
        Long delete = getSet().delete(ROOM_KEY + roomId, wsPrincipal.getName());
        if(delete > 0){
            BaseMessage baseMessage = new BaseMessage(wsPrincipal);
            baseMessage.setMsgType(MessageType.EVENT_USER_LEFT);
            baseMessage.setContent("离开房间");
            RoomMsgSender.sendPublicMsg(roomId, baseMessage);
        }
        return false;
    }

    //退出所有房间 未实现
    public static void delUserAllRoom(WSPrincipal wsPrincipal) {

    }

    //获取房间内指定用户
    public static WSPrincipal getPrincipal(String roomId, String userId){
        if(getSet().hasKey(ROOM_KEY + roomId, userId)){
            WSPrincipal  w = getSet().get(ROOM_KEY + roomId, userId);
            return w;
        }
        return null;
    }


    //获取房间内指定用户
    public static boolean hasPrincipal(String roomId, String userId){
        return getSet().hasKey(ROOM_KEY + roomId, userId);
    }


    // 获取房间内所有用户
    public static List<WSPrincipal> getUsers(String roomId, int start, int limit){
        List<WSPrincipal> members = getSet().values(ROOM_KEY + roomId);
        return members;
    }


    public static Long getUserNum(String roomId){
        Long size = getSet().size(ROOM_KEY + roomId);
        if(size != null){
            return size;
        }
        return 0l;
    }

    /**
     * 禁言用户
     * @param wsPrincipal
     * @param roomId
     */
    public static void forbiddenUser(WSPrincipal wsPrincipal, String roomId){
        if(wsPrincipal != null){
            wsPrincipal.setForbidden(true);
            UserForbidden.forbidden(roomId, wsPrincipal.getName());
        }
    }

    /**
     * 解禁用户发言
     * @param wsPrincipal
     * @param roomId
     */
    public static void unForbiddenUser(WSPrincipal wsPrincipal, String roomId){
        if(wsPrincipal != null){
            wsPrincipal.setForbidden(false);
            UserForbidden.unForbidden(roomId, wsPrincipal.getName());
        }
    }


    public static boolean doRoomEvent(BaseMessage jsonObject, String roomId, WSPrincipal currentPrincipal){
        if(currentPrincipal.isAdmin()){
            MessageType msgType = jsonObject.getMsgType();
            if(msgType != null) {
                switch (msgType) {
                    //全体禁言
                    case EVENT_ALL_SILENT:
                        RoomStatusTool.allForbidden(roomId);
                        break;
                    //全体解禁
                    case EVENT_ALL_LIBERATE:
                        RoomStatusTool.unAllforbidden(roomId);
                        break;
                    //添加管理员
                    case EVENT_ADD_ADMIN:
                        getPrincipal(roomId, jsonObject.getToUser()).setAdmin(true);
                        break;
                    //禁言某人
                    case EVENT_USER_SILENT:
                        forbiddenUser(getPrincipal(roomId, jsonObject.getToUser()), roomId);
                        break;
                    //解禁禁言某人
                    case EVENT_USER_UNSILENT:
                        unForbiddenUser(getPrincipal(roomId, jsonObject.getToUser()), roomId);
                        break;
                    default:
                        break;
                }
                return true;
            }
        }else {
            MessageType msgType = jsonObject.getMsgType();
            if(!msgType.equals(MessageType.TEXT)){
                return false;
            }
        }
        return true;
    }

}
